define(function (require) {
var zsApp = require('app');
require('rolebyrole');
require('rolebyuser');
zsApp.controller( 'rolebyController',["$scope","$http","$state","$stateParams","$window","$timeout","$rootScope","$cookieStore","$filter","promptCache", function($scope,$http,$state,$stateParams,$window,$timeout,$rootScope,$cookieStore,$filter,promptCache){
	
	//从cookie取roleId
	var roleIdCookieNew = $cookieStore.get('roleIdcookie');
	$scope.roleIdnew = roleIdCookieNew ;
	//console.log($scope.roleIdnew);
	//从cookie取userList
//	var userListCookieNew = $cookieStore.get('userListcookie');
//	$scope.userListnew = userListCookieNew;
//	console.log($scope.userListnew);
	//user多选框
	$scope.selected = [];
    $scope.selectedTags = [];  
	//$scope.bindingUserdata = {};
    var updateSelected = function(action,userId,name){  
        if(action == 'add' && $scope.selected.indexOf(userId) == -1){  
            $scope.selected.push(userId);  
            $scope.selectedTags.push(name);          
        }
        if(action == 'remove' && $scope.selected.indexOf(userId)!=-1){  
            var idx = $scope.selected.indexOf(userId);  
            $scope.selected.splice(idx,1);  
            $scope.selectedTags.splice(idx,1);  
        }   
    }  
    $scope.updateSelection = function($event, userId){  
        var checkbox = $event.target;  
        var action = (checkbox.checked?'add':'remove');  
        updateSelected(action,userId,checkbox.name);  
        console.log($scope.selected);
    }  
   
    $scope.isSelected = function(id){  
        return $scope.selected.indexOf(id)>=0;    	
    }  
    
    
   	$scope.userAll = function(user_tabVal){ 
   		console.log($scope.selected);
	}
    
	//用户绑定
	$scope.binding = function(){
		
		var bindingUserdata = {
				userId 		: $scope.selected,
				userName   	: $scope.selectedTags,
				roleId      : $scope.roleIdnew  
		}
		console.log(bindingUserdata)
		if(bindingUserdata.userId == ""){
			//用户提示
			$rootScope.prompt = true;
			$rootScope.promptStatement = "请选择user用户";
			$timeout(function () {
				$rootScope.prompt = false;
			}, 1500);
		}else{
			$http({
				url      : $scope.zsUrl+'digitalhome-boss/admin/roles/addUserRole',
				method   : 'POST',
				data     : JSON.stringify(bindingUserdata),/* 将JSON发送给服务器 */
				dataType : 'json',
				contentType : 'application/json;charset=UTF-8', 
			}).then(function success(res) {
				console.log(res.status);
			
			
			if( '0000' == res.data.result_code ){
				promptCache.successPrompt(res.data.message);
				$rootScope.paginationConfuser.onChange();
				$rootScope.paginationConfrole.onChange();
			}else{
				promptCache.successPrompt(res.data.message);	
			}
//				$timeout(function(){  
//                  $scope.$apply();
//              },100);
				//$state.reload();
			},
			function error(err){
				console.log(err);
			});
		}
			
	}
	
	//role多选框
	$scope.roleselected = [];  
	
    var roleupdateSelected = function(action,userRoleId){  
        if(action == 'add' && $scope.roleselected.indexOf(userRoleId) == -1){  
            $scope.roleselected.push(userRoleId);  
        
        }
        if(action == 'remove' && $scope.roleselected.indexOf(userRoleId)!=-1){  
            var idx = $scope.roleselected.indexOf(userRoleId);  
            $scope.roleselected.splice(idx,1);   
        }  
    }  
 
    $scope.roleupdateSelection = function($event,userRoleId){  
        var checkbox = $event.target;  
        var action = (checkbox.checked?'add':'remove');  
        roleupdateSelected(action,userRoleId); 
        console.log($scope.roleselected);
    }  
   
    $scope.roleisSelected = function(id){  
        return $scope.roleselected.indexOf(id)>=0;  
        	
    } 
	//解除绑定
	$scope.unbound = function($event, $index){
		
		var unboundRoledata = {
    		userRoleId : $scope.roleselected,
    	}
		if(unboundRoledata.userRoleId == ""){
			$rootScope.prompt = true;
			$rootScope.promptStatement = "请选择已关联用户";
			$timeout(function () {
				$rootScope.prompt = false;
			}, 1500);
		}else{
			$http({
				url      : $scope.zsUrl+'digitalhome-boss/admin/roles/deleteUserRoles',
				method   : 'POST',
				data     : JSON.stringify(unboundRoledata),/* 将JSON发送给服务器 */
				dataType : 'json',
				contentType : 'application/json;charset=UTF-8', 
			}).then(function success(res) {
				console.log(res.status);
//				$state.reload();
////				//$state.go("index.roleby",{},{reload:true});
//				$timeout(function(){  
//                  $scope.$apply();    
//              },100);
               
			if( '0000' == res.data.result_code ){
				promptCache.successPrompt(res.data.message);
				 $rootScope.paginationConfuser.onChange();
				$rootScope.paginationConfrole.onChange();
			}else{
				promptCache.successPrompt(res.data.message);	
			}
			},
			function error(err){
				console.log(err);
			});	
		}
		
	}
	
	function loadtable(){
		$rootScope.paginationConfrole.onChange();
		$rootScope.paginationConfuser.onChange();
	}
	
	   		        
}])
})